package demo1;

import java.util.Objects;

class Money implements Cloneable
{
    public double Money=12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


class Student implements Cloneable{
    public String name;
    public Money m=new Money();
//    public Student(String name) {
//        this.name = name;
//
//    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //只是克隆了Student对象
        Student student=(Student) super.clone();
        //克隆了 Student对象里面的money对象
        student.m=(Money)this.m.clone();
        //返回Student对象
        return student;
        //return super.clone();
    }

    @Override
    public boolean equals(Object obj) {
//       Student student=(Student) obj;
//        if (obj == null) {
//            return false ;
//        }
//
//        else if(this.name.equals(student.name))
//           return true;
//       else
//           return false;


            if (obj == null) {
                return false ;
            }
            if(this == obj) {
                return true ;
            }
               // 不是student类对象
            if (!(obj instanceof Student)) {
                return false ;
            }
            Student student = (Student) obj ; // 向下转型，比较属性值
            return this.name.equals(student.name);
        }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student student1=new Student();
        student1.name="zhangsan";

        Student student2=new Student();
        student2.name="zhangsan";
        System.out.println(student1==student2);
        boolean flg=student1.equals(student2);
        System.out.println("flg:"+flg);
        System.out.println("==================");
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());
    }


    public static void main2(String[] args) throws CloneNotSupportedException {
        Student student1=new Student();
        Student student2=(Student) student1.clone();
        System.out.println(student1.m.Money);
        System.out.println(student2.m.Money);
        System.out.println("===========");
        student1.m.Money=99;
        System.out.println(student1.m.Money);
        System.out.println(student2.m.Money);
    }


    public static void main1(String[] args) throws CloneNotSupportedException {
        Student student1=new Student();
        student1.name="bittejiuyeke";
        Student student2=(Student) student1.clone();
        System.out.println(student1);
        System.out.println(student2);
        System.out.println("===================");
        Student student5=(Student) student1.clone();
        student1.name="abc";
        System.out.println(student1);
        System.out.println(student5);
        System.out.println("==================");
        Student student3=(Student) student1.clone();
        System.out.println(student1.m.Money);
        System.out.println(student3.m.Money);
        System.out.println("==================");
        Student student4=(Student) student1.clone();
        student1.m.Money=99;
        System.out.println(student1.m.Money);
        System.out.println(student4.m.Money);


    }
}
